
#import "GesturePasswordView.h"
#import "GesturePasswordButton.h"
#import "TentacleView.h"
#import "KeychainItemWrapper.h"


@implementation GesturePasswordView {
    NSMutableArray * buttonArray;
    
    CGPoint lineStartPoint;
    CGPoint lineEndPoint;
    
}
@synthesize imgView;
@synthesize forgetButton;
@synthesize changeButton;
@synthesize backButton;

@synthesize tentacleView;
@synthesize state;
@synthesize gesturePasswordDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        buttonArray = [[NSMutableArray alloc]initWithCapacity:0];
        
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2-160, frame.size.height/2-80, 320, 320)];
        if (UI_SCREEN_HEIGHT == 480) {
            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - 10, 320, 320);
        }
        for (int i=0; i<9; i++) {
            NSInteger row = i/3;
            NSInteger col = i%3;
            // Button Frame
            
            NSInteger distance = 320/3;
            NSInteger size = distance/1.5;
            NSInteger margin = size/4;
            GesturePasswordButton * gesturePasswordButton = [[GesturePasswordButton alloc]initWithFrame:CGRectMake(col*distance+margin, row*distance, size, size)];
            [gesturePasswordButton setTag:i];
            [view addSubview:gesturePasswordButton];
            [buttonArray addObject:gesturePasswordButton];
        }
        frame.origin.y=0;
        [self addSubview:view];
        tentacleView = [[TentacleView alloc]initWithFrame:view.frame];
        [tentacleView setButtonArray:buttonArray];
        [tentacleView setTouchBeginDelegate:self];
        [self addSubview:tentacleView];
        
        state = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width/2-140, frame.size.height/2-120, 280, 30)];
        [state setTextAlignment:NSTextAlignmentCenter];
        [state setFont:[UIFont systemFontOfSize:14.f]];
        [self addSubview:state];
        
        
        imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width/2-35, frame.size.width/2-80, 70, 70)];
        if (UI_SCREEN_HEIGHT == 480) {
            imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width/2-35, frame.size.height/2-200, 70, 70)];
        }
        [imgView.layer setCornerRadius:35];
        [imgView.layer setBorderColor:[UIColor clearColor].CGColor];
        [imgView.layer setBorderWidth:3];
        imgView.layer.masksToBounds = YES;
        imgView.image = [UIImage imageNamed:@"ic_锁掌柜_70"];
        [self addSubview:imgView];
        
        forgetButton = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height/2+220, frame.size.width, 30)];
        changeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height/2+220, frame.size.width, 30)];
        backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, frame.size.height/2 + 220, 80, 30)];
        if (UI_SCREEN_HEIGHT == 480) {
            forgetButton = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height/2+200, frame.size.width, 30)];
            changeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height/2+200, frame.size.width, 30)];
             backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, frame.size.height/2 + 200, 80, 30)];
        }
        [forgetButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [forgetButton setTitle:NSLocalizedString(@"forgetGuesture", nil) forState:UIControlStateNormal];
        [forgetButton addTarget:self action:@selector(forget) forControlEvents:UIControlEventTouchDown];
        forgetButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:forgetButton];
        
        [changeButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [changeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [changeButton setTitle:NSLocalizedString(@"modifyGuesture", nil) forState:UIControlStateNormal];
        [changeButton addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchDown];
        [changeButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [self addSubview:changeButton];
        
        [backButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(forget) forControlEvents:UIControlEventTouchDown];
        [backButton setTitle:NSLocalizedString(@"back", nil) forState:0];
        [self addSubview:backButton];
    }
    
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGFloat colors[] =
    {
        134 / 255.0, 157 / 255.0, 147 / 255.0, 1.00,
        3 / 255.0,  3 / 255.0, 37 / 255.0, 1.00,
    };
    CGGradientRef gradient = CGGradientCreateWithColorComponents
    (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
    CGColorSpaceRelease(rgb);
    CGContextDrawLinearGradient(context, gradient,CGPointMake
                                (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),
                                kCGGradientDrawsBeforeStartLocation);
    
    CGGradientRelease(gradient);
}

- (void)gestureTouchBegin {
    [self.state setText:@""];
}

-(void)setHiddenOrNot:(BOOL)hiddenOrNot
{
    if (hiddenOrNot) {
        forgetButton.enabled = YES;
        changeButton.hidden = YES;
        backButton.hidden = YES;
        backButton.enabled = NO;
        changeButton.enabled = NO;
    }
    else
    {
        forgetButton.hidden = YES;
        changeButton.hidden = NO;
        changeButton.enabled = NO;
        forgetButton.enabled = NO;
        
    }
}

-(void)forget{
    [gesturePasswordDelegate forget];
}

-(void)change{
    [gesturePasswordDelegate change];
}


@end
